#!/usr/bin/env python3.8
# -*- encoding: utf-8 -*-
'''
@File    :   utils.py
@Time    :   2021/10/11 15:25:36
@Author  :   windstarry 
@Version :   1.0
'''
# here put the import lib
from flask_mail import Message
from flask import render_template,current_app,request
from app.ext import mail
from threading import Thread
from urllib.parse import urlparse, urljoin


def is_safe_url(target):
    # 获取程序内的主机url
    ref_url = urlparse(request.host_url)
    # 将目标URl转换为绝对路径
    test_url = urlparse(urljoin(request.host_url, target))
    # 验证是否属于内部url
    return test_url.scheme in ('http', 'https') and ref_url.netloc == test_url.netloc 




def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)


def send_email(subject, sender, recipients, text_body, html_body):
    #创建消息
    msg = Message(subject, sender=sender, recipients=recipients)
    msg.body = text_body
    msg.html = html_body
    #异步发送邮件
    Thread(target=send_async_email,args=(current_app._get_current_object(), msg)).start()

